package com.one.rope.mvp.web.service.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.one.rope.mvp.basecore.bean.ResponseObject;
import com.one.rope.mvp.basecore.third.wx.common.WxGetUserInfoRes;
import com.one.rope.mvp.basecore.third.wx.common.WxSendCustomTextMsgReq;
import com.one.rope.mvp.basecore.third.wx.common.WxSendMsgRes;
import com.one.rope.mvp.basecore.third.wx.common.WxSendTemplateMsgReq;
import com.one.rope.mvp.basecore.third.wx.official.WxOfficeUtil;
import com.one.rope.mvp.basecore.util.StringUtils;
import com.one.rope.mvp.web.business.dao.MemberSubscribeRecordMapper;
import com.one.rope.mvp.web.business.entity.MemberSubscribeRecord;
import com.one.rope.mvp.web.common.dto.common.wx.GenerateQrcodeVO;
import com.one.rope.mvp.web.common.redis.CacheDataRedisOperator;
import com.one.rope.mvp.web.config.CommonConfig;
import com.one.rope.mvp.web.config.GlobalConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 微信公众号服务
 * <p>
 *
 * @author ： Weijian.Liu
 * @date ： 2020/3/28 10:27
 */
@Service
public class WxOfficialService {

  @Autowired
  CommonConfig commonConfig;

  @Autowired
  MemberSubscribeRecordMapper memberSubscribeRecordMapper;

  public ResponseObject<GenerateQrcodeVO> generateQrcode(String sceneStr) throws Exception{
    GenerateQrcodeVO generateQrcodeVO = new GenerateQrcodeVO();
    String qrCodeUrl = WxOfficeUtil.createQrcode(getOfficialAccessToken(), sceneStr);
    generateQrcodeVO.setQrCodeUrl(qrCodeUrl);
    return new ResponseObject<>(generateQrcodeVO);
  }

  /**
   * 获取公众号接口调用AccessToken，有效时间7200s
   * @return
   * @throws Exception
   */
  public String getOfficialAccessToken() throws Exception{
    String accessToken = CacheDataRedisOperator.getOfficialAccessToken();
    if(StringUtils.isNullOrEmpty(accessToken)){
      String token = WxOfficeUtil.getAccessToken(commonConfig.getWxOfficialAppId(), commonConfig.getWxOfficialAppSecret());
      CacheDataRedisOperator.saveOfficialAccessToken(token);
      accessToken = token;
    }
    return accessToken;
  }

  /**
   * 发送客服文本消息
   * @param msgReq
   * @return
   */
  public WxSendMsgRes sendCustomerTextMsg(WxSendCustomTextMsgReq msgReq) throws Exception{
    msgReq.setAccessToken(getOfficialAccessToken());
    WxSendMsgRes wxSendMsgRes = WxOfficeUtil.sendCustomerTextMsg(msgReq);
    return wxSendMsgRes;
  }

  /**
   * 获取公众号用户基本信息
   * @param openId
   * @return
   */
  public WxGetUserInfoRes getUserInfo(String openId) throws Exception{
    WxGetUserInfoRes getUserInfoRes = WxOfficeUtil.getUserInfo(getOfficialAccessToken(), openId);
    return getUserInfoRes;
  }

  /**
   * 保存公众号关注记录
   */
  public void saveMemberSubscribeRecord(MemberSubscribeRecord subscribeRecord){
    MemberSubscribeRecord dbRecord = memberSubscribeRecordMapper.selectOne(
        new QueryWrapper<MemberSubscribeRecord>().eq("wx_union_id", subscribeRecord.getWxUnionId())
    );
    if(null == dbRecord){
      memberSubscribeRecordMapper.insert(subscribeRecord);
    }else{
      subscribeRecord.setId(dbRecord.getId());
      memberSubscribeRecordMapper.updateById(subscribeRecord);
    }
  }

  /**
   * 发送模板文本消息
   * @param msgReq
   * @return
   * @throws Exception
   */
  public WxSendMsgRes sendTemplateTextMsg(WxSendTemplateMsgReq msgReq) throws Exception{
    msgReq.setAccessToken(getOfficialAccessToken());
    msgReq.getMiniprogram().setAppid(commonConfig.getWxMiniProgramSkipAppId());
    msgReq.setUrl(GlobalConfig.OFFICIAL_URL);
    msgReq.setTemplateId(commonConfig.getWxOfficialTemplateMsgId());
    WxSendMsgRes wxSendMsgRes = WxOfficeUtil.sendTemplateTextMsg(msgReq);
    return wxSendMsgRes;
  }
}
